home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
public
/
bit
/
src
/
iris.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
6KB
|
237 lines
/*
* $Id: iris.c,v 0.91 1994/02/20 00:52:47 zhao Pre-Release $
*
*. This file is part of BIT shareware package. After the two weeks of
* free evaluation period, you are encouraged (required) to register
* your copy for a small registration fee, which is $35 for personal use
* and $50 for commercial, government and institutional use.
*
* Copyright(c) 1993, 1994 by T.C. Zhao.
* All rights reserved.
*
* Permission to use, copy, and distribute this software in its entirety
* for non-commercial purposes is hereby granted, provided that the
* above shareware and copyright notices and this permission notice
* appear in all copies and their documentation.
*
* This software may be modified for your own use, but modified versions
* may not be distributed without prior consent of the author.
*
* This software is provided "as is" without expressed or implied
* warranty of any kind.
*
*.
* IRIS RGB stuff. Using Paul Haeberli's library as found in ~4Dgifts.
*
*/
#if !defined(lint) && defined(F_ID)
char *id_iris = "$Id: iris.c,v 0.91 1994/02/20 00:52:47 zhao Pre-Release $";
#endif
#include "bit.h"
#include "gl/image.h"
#include "dmalloc.h"
/*************** local variable ***************************/
static IMAGE *iris_head; /* IRIS RGB header info */
static long iris_rlines; /* number of lines read so far */
static const char *irisload = "Load_IRIS";
static int iriserr; /* to prevent excess error reporting */
/************ there is no prototyping in gl/image.h. do it here ****/
extern int i_seterror(void (*p) (const char *));
extern int getrow(IMAGE *, unsigned short *, unsigned, unsigned);
extern int putrow(IMAGE *, unsigned short *, unsigned, unsigned);
extern int iclose(IMAGE *);
/**************************************************************
* take over the error handling from imagelib.
**************************************************************/
static void
err_func(const char *s)
{
Bark(irisload, s);
iriserr++;
}
/*************************************************************
* get IRIS image type and dimension
*************************************************************/
int
RGB_desc(IPTR a)
{
i_seterror(err_func);
iriserr = 0;
iris_head = iopen(a->ifile, "r", 0, 0, 0, 0, 0);
if (!iris_head)
{
Bark("IRIS_desc", a->ifile);
return -1;
}
a->w = iris_head->xsize;
a->h = iris_head->ysize;
if (iris_head->zsize >= 3)
a->type = T_RGBA;
else if (iris_head->zsize == 1)
a->type = T_GRAY; /* not sure this is correct */
else
a->type = T_CMAP; /* not sure about this either */
set_iformat_info(a, (ISRLE(iris_head->type)) ? "RLE compressed" : "");
return iriserr > 1 ? -1 : 0;
}
/***********************************************************
* load all pixels by calling getrow repeatedly
************************************************************/
int
RGB_load(IPTR im)
{
register rgba_t *head;
register unsigned short *rbuf, *gbuf = 0, *bbuf = 0, *rs;
register size_t y;
unsigned short *rh = 0, *gh = 0, *bh = 0;
int err = 0;
iris_rlines = progress_report("Loading IRIS...", im->h);
head = im->raster;
y = sizeof(unsigned short) * im->w;
if (IS_RGBA(im))
{
if ((err = !(rh = rbuf = malloc(y)) ||
!(gh = gbuf = malloc(y)) ||
!(bh = bbuf = malloc(y))))
{
Bark(irisload, "malloc failed");
Free(rh);
Free(gh);
Free(bh);
return -1;
}
/* Load raster one row at a time and pack it on the fly */
for (y = err = 0; !err && y < im->h; y++)
{
err = (iriserr > 2) ||
(getrow(iris_head, (rbuf = rh), y, 0) < 0) ||
(getrow(iris_head, (gbuf = gh), y, 1) < 0) ||
(getrow(iris_head, (bbuf = bh), y, 2) < 0);
for (rs = rh + im->w; rbuf < rs; rbuf++, gbuf++, bbuf++)
{
*head++ = Pack(*rbuf, *gbuf, *bbuf);
}
REPORT(y, iris_rlines);
}
free(rh);
free(gh);
free(bh);
}
else if (IS_RGBGRAY(im))
{
rh = rbuf = malloc(y);
for (y = err = 0; !err && y < im->h; y++)
{
err = (iriserr > 2) ||
(getrow(iris_head, (rbuf = rh), y, 0) < 0);
for (rs = rh + im->w; rbuf < rs; rbuf++, head++)
{
*head = Pack(*rbuf, *rbuf, *rbuf);
}
REPORT(y, iris_rlines);
}
free(rh);
}
else
{
Bark(irisload, "%s: Bad image type", im->ifile);
err = -1;
}
err = (iclose(iris_head) || err);
/*
* for all memory not malloc'ed directly by bit, should use Tfree in case
* malloc wrapper is used in bit
*/
Tfree(iris_head);
remove_progress_report();
return y;
}
/****************************************************************
* write image in IRIS format to disk
*****************************************************************/
int
RGB_dump(IPTR im)
{
register rgba_t *head;
register unsigned short *rbuf, *gbuf, *bbuf, *rs;
register size_t y;
unsigned short *rh, *gh = 0, *bh = 0;
int z;
z = IS_GRAY(im) ? 1 : 3;
/* in case we write IRIS directly */
i_seterror(err_func);
/* always write RLE compressed */
if (!(iris_head = iopen(im->ofile, "w", RLE(1), 3, im->w, im->h, z)))
return -1;
iris_rlines = progress_report("Writing IRIS...", im->h);
head = im->raster;
y = sizeof(*rbuf) * im->w;
if (z == 3)
{
if (!(rh = rbuf = malloc(y)) ||
!(gh = gbuf = malloc(y)) ||
!(bh = bbuf = malloc(y)))
{
Bark("IRIS_dump", "malloc failed");
Free(rh);
Free(gh);
Free(bh);
return -1;
}
/* get one row a time and call putrow to output */
for (y = 0; y < im->h; y++)
{
for (rs = rh + im->w; rbuf < rs; head++, rbuf++, gbuf++, bbuf++)
{
Unpack(*head, *rbuf, *gbuf, *bbuf);
}
putrow(iris_head, (rbuf = rh), y, 0);
putrow(iris_head, (gbuf = gh), y, 1);
putrow(iris_head, (bbuf = bh), y, 2);
REPORT(y, iris_rlines);
}
free(rh);
free(gh);
free(bh);
}
else
{ /* grayscale */
rh = rbuf = malloc(y);
for (y = 0; y < im->h; y++)
{
for (rs = rh + im->w; rbuf < rs; head++, rbuf++)
*rbuf = (*head & (PCMAX - 1));
putrow(iris_head, (rbuf = rh), y, 0);
REPORT(y, iris_rlines);
}
free(rh);
}
iclose(iris_head);
Tfree(iris_head);
remove_progress_report();
return 0;
}